﻿package classes.store.storeclasses 
{
	import com.mttang.event.JEvent;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite; 
	import flash.utils.getDefinitionByName;
	 
	import flash.filters.GlowFilter;
	import classes.bag.BagRockMan;
	import classes.store.StoreRockman;
	import classes.savedata.RockmanData;
	import classes.GameSetting;
	
	import flash.events.MouseEvent;
	
	import com.mttang.event.CommonEvent;
	
	/**
	 * 背包类 
	 * @author rock
	 */
	public class Knapsack extends Sprite
	{  
		public var knapsackArr:Array = []; //存放背包里面所有的物品
		private var _aNewBagArr:Array = [];//显示后赋值的新ID保存 
		private var _mKnapsackCon:MovieClip;//包含所有商品的容器   
		private var _nKpsGoodsX:int=0;//临时背包显示位置X
		private var _nKpsGoodsY:int = 0;//临时背包显示位置Y
		private var _nKpsGoodsIndeX:int = 0;//临时背包X轴索引
		private var _nKpsGoodsIndeY:int = 0;//临时背包Y轴索引
		
		private var  _aNewKnapsackArr:Array= [];//删除后产生的新数组
		private var _nGoodsIndex:Number = 0;//要添加的元素相同时，记录同一元素在数组中的索引位置,方便在背包里重新显示该物品 数量
		private var _nGoodsNum:Number = 0;//记载同一样的物品应相加数量
		private var _nGoodsMoney:Number = 0;//保存所选中物品购买所需的金钱 
			
		private var _oDeleteGoods:Object;   //参数：{ "name":"", "petid":"", "price":"", "level":"", "status":"", "anti":"","skills":"","id":""};//选中的需要删除的物品 
		private var _bDelterFlg:Boolean = false;//是否有选中的要的物品  
		
		//private var  scrollCls:ScrollCls;   
		
		private var _gDelGlowFtr:GlowFilter;//点击时的滤镜
		private var _aDelFtrArr:Array = [];
		private var _mDelGoodCkMc:MovieClip;//记载上一次点击过的商品
		
		//private var _nBagLength:Number = 28;//背包存放物品的总数
		 
		 
		public function Knapsack():void
		{ 
		    //一个容器，把所有商品丢进去 
			_mKnapsackCon = new MovieClip();
			_mKnapsackCon.x = 110;
			_mKnapsackCon.y = 95;
			addChild(_mKnapsackCon); 
			
			//实例化滚动条
			//scrollCls=new ScrollCls();
		} 
		//收藏此物品 
		public function favorite():void
		{
			knapsackArr.push(ShopList.selectGodds); 
		}
		
		//加入此物品到背包
		public function show():void
		{       
			//生成滤镜
			creatFilter();
			clearData();//清理容器内所有对象  
			
		    //物品在背包内的显示位置逻辑处理 
			if (knapsackArr.length != 0)
			{   
			    for (var i:int = 0; i < knapsackArr.length; i++)
			    {  
				    if (i % 2 == 0&& i != 0)
				    {   
					    _nKpsGoodsIndeX = 0; 
					    _nKpsGoodsIndeY++;  
				    } 
				    _nKpsGoodsX = _nKpsGoodsIndeX * 62;//X中间间隔10
					_nKpsGoodsY = _nKpsGoodsIndeY * 61;	
				    _nKpsGoodsIndeX++; 
				    setData(knapsackArr[i].name,knapsackArr[i].petid,knapsackArr[i].price,knapsackArr[i].level,knapsackArr[i].status,knapsackArr[i].anti,knapsackArr[i].skills,_nKpsGoodsX,_nKpsGoodsY,i);
			    }   
				//addChild(scrollCls); 
			    //scrollCls.x = 100; 
			   // scrollCls.y = 85;
				//showScroll(_mKnapsackCon,120,175);//显示滚动条
				
				//给临时背包重新赋值
				knapsackArr = [];
				for (var j:uint = 0; j < _aNewBagArr.length; j++ )
				{
				    knapsackArr.push(_aNewBagArr[j]);	
				}
				_aNewBagArr = [];
			}
		} 
		
		//物品类名，类型，名称，ID，宠物币价格，数量，物品X，物品Y
		public function setData(_name:String,_petid:int,_price:int,_level:int,_status:int,_anti:int,_skills:String,_goodsX:int,_goodsY:int,_id:int):void
		{   
			try
			{
			//var _mKpsMc:MovieClip = new KnapsackImg();//商品抽屉
			var _mKpsGoodsMc:MovieClip = new (GameSetting.petRsc.contentLoaderInfo.applicationDomain.getDefinition(_name+""+_petid) as Class)();
			// var _mKpsGoodsMc:MovieClip=new (getDefinitionByName(_name+""+_petid) as Class)();
			
			 //设置物品显示位置
			 _mKpsGoodsMc.x = _goodsX+_mKpsGoodsMc.width/2;
			 _mKpsGoodsMc.y = _goodsY+_mKpsGoodsMc.height / 2; 
			 _mKpsGoodsMc.scaleX *= .7;
			 _mKpsGoodsMc.scaleY *= .7;  
			 _mKpsGoodsMc.mouseChildren = false;
			 
			 //_mKpsMc.addChild(_mKpsGoodsMc); 
			 _mKnapsackCon.addChild(_mKpsGoodsMc);
			 
			//赋值 
			_mKpsGoodsMc.name = _name;
			_mKpsGoodsMc.petid = _petid;
			_mKpsGoodsMc.price = _price; 
			_mKpsGoodsMc.level = _level;
			_mKpsGoodsMc.status = _status;
			_mKpsGoodsMc.anti = _anti;
			_mKpsGoodsMc.skills = _skills;
			_mKpsGoodsMc.id = _id;
			
			var obj:Object = new Object();
			obj.name = _name;
			obj.petid = _petid;
			obj.price = _price; 
			obj.level = _level;
			obj.status = _status;
			obj.anti = _anti;
			obj.skills = _skills;
			obj.id = _id;
			_aNewBagArr.push(obj);
			  
			_mKnapsackCon.addEventListener(MouseEvent.CLICK, _mKpsGoodsMc_ckHandler); 
			}catch (e:*)
			{
			    trace(e.type);	
			}
		}
		private function _mKpsGoodsMc_ckHandler(e:MouseEvent):void
		{
			_bDelterFlg = true;
			
			//删除上一个物品的滤镜效果  
			_aDelFtrArr = [];
			if (_mDelGoodCkMc)_mDelGoodCkMc.filters = _aDelFtrArr;
			
			//添加当前物品的滤镜效果
			_aDelFtrArr.push(_gDelGlowFtr); 
			_mDelGoodCkMc = e.target as MovieClip; //保存上一个点击物品对象 
			_mDelGoodCkMc.filters = _aDelFtrArr; 
			
			_oDeleteGoods=new Object();
			_oDeleteGoods.name = e.target.name;
			_oDeleteGoods.petid = e.target.petid;
			_oDeleteGoods.price = e.target.price; 
			_oDeleteGoods.level = e.target.level;
			_oDeleteGoods.status = e.target.status;
			_oDeleteGoods.anti = e.target.anti;
			_oDeleteGoods.skills = e.target.skills;
			_oDeleteGoods.id = e.target.id;
			trace("要删除元素的名字>>>:" + _oDeleteGoods.id) 
		}
		
		//购买物品
		public function buyGoods():void
		{
		    if (knapsackArr.length != 0)
			{ 
				for (var i:uint = 0; i < knapsackArr.length; i++)
				{  
				    StoreRockman._saveData.bag.push(knapsackArr[i]);//将买的物品添加到背包 
					StoreRockman._saveData.user.money-=parseInt(knapsackArr[i].price)
					if (i == knapsackArr.length - 1)
					{
					    clearData(); 
					    knapsackArr = [];
					}
				}
			    RockmanData.saveData(StoreRockman._saveData);	
			    //showScroll(_mKnapsackCon,90,260);//显示滚动条 
			}
		}
		//删除选中物品 
		public function deleteGoods():void
		{ 
			if (_bDelterFlg)//如果有选中的
			{
			    clearData();	 
				for (var j:uint = 0; j < knapsackArr.length; j++)
				{
				    if (_oDeleteGoods.id != knapsackArr[j].id)
					{    
						_aNewKnapsackArr.push(knapsackArr[j]);
					} 	
				}
				//将新数组赋值给背包数组
				knapsackArr=[]; 
				for(var i:Number=0;i<_aNewKnapsackArr.length;i++)
				{
					knapsackArr.push(_aNewKnapsackArr[i]);
				}
				show();
				_aNewKnapsackArr=[];
				_bDelterFlg = false; 
			} 
			else
			{
			    trace("您没有物品被选中!")	
				var transGoods:TransactionsGoods = new TransactionsGoods();
				transGoods.showKpsDelAlert();
				addChild(transGoods);
			}
		}
		//创建滤镜
		private function creatFilter():void
		{
		   	_gDelGlowFtr = new GlowFilter(0xFFFFFF,1,10,10);
		}
		 
		//删除背包容器中的所有物品
		public function clearData():void
		{   trace("------购买哦哦哦 ------")
		    var i:Number = 0;
			while(i<_mKnapsackCon.numChildren)
			{  
			    _mKnapsackCon.removeChild(_mKnapsackCon.getChildAt(i));  
			}
			i++;  
			
			_nKpsGoodsX=0;//临时背包显示位置X
		    _nKpsGoodsY = 0;//临时背包显示位置Y
		    _nKpsGoodsIndeX = 0;//临时背包X轴索引
		    _nKpsGoodsIndeY = 0;//临时背包Y轴索引
		}
		//生成滚动条
		private function showScroll(_displayObj:DisplayObject,_objWidht:Number,_objHeight:Number):void
		{    
			//scrollCls.visible = true; 
			//scrollCls.setSizeWH(_objWidht,_objHeight);
			//scrollCls.show(_displayObj); 
		} 
	}

}